message(STATUS "python")

# set
set(SOURCE)
set(OBJECT)
set(BINARY pynndeploy)
set(DIRECTORY python)
set(DEPEND_LIBRARY)
set(SYSTEM_LIBRARY)
set(THIRD_PARTY_LIBRARY)

# 添加版本信息定义
set(PACKAGE_VERSION ${NNDEPLOY_VERSION})
add_definitions(-DVERSION_INFO="${PACKAGE_VERSION}")

# 添加 pybind11 子目录
add_subdirectory(${ROOT_PATH}/third_party/pybind11)

# include
include_directories(${ROOT_PATH}/python/src)
include_directories(${pybind11_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS})

# SOURCE
file(GLOB_RECURSE  PYTHON_SOURCE
  "${ROOT_PATH}/python/src/*.h"
  "${ROOT_PATH}/python/src/*.cc"
)
set(SOURCE ${SOURCE} ${PYTHON_SOURCE})

# 创建 Python 模块
pybind11_add_module(${BINARY} ${SOURCE})

# 属性
set_target_properties(${BINARY} PROPERTIES OUTPUT_NAME "_nndeploy_internal")
set_target_properties(${BINARY} PROPERTIES PREFIX "" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/nndeploy")
set_property(TARGET ${BINARY} PROPERTY FOLDER ${DIRECTORY})

# link
# target_link_libraries(${BINARY} PUBLIC nndeploy_framework)
# DEPEND_LIBRARY
list(APPEND DEPEND_LIBRARY ${NNDEPLOY_FRAMEWORK_BINARY})
list(APPEND DEPEND_LIBRARY ${NNDEPLOY_DEPEND_LIBRARY})
list(APPEND DEPEND_LIBRARY ${NNDEPLOY_PYTHON_DEPEND_LIBRARY})
target_link_libraries(${BINARY} PUBLIC  ${DEPEND_LIBRARY})

# SYSTEM_LIBRARY
list(APPEND SYSTEM_LIBRARY ${NNDEPLOY_SYSTEM_LIBRARY})
list(APPEND SYSTEM_LIBRARY ${NNDEPLOY_PYTHON_SYSTEM_LIBRARY})
target_link_libraries(${BINARY} PUBLIC  ${SYSTEM_LIBRARY})

# THIRD_PARTY_LIBRARY
list(APPEND THIRD_PARTY_LIBRARY ${NNDEPLOY_THIRD_PARTY_LIBRARY})
list(APPEND THIRD_PARTY_LIBRARY ${NNDEPLOY_PYTHON_THIRD_PARTY_LIBRARY})
list(APPEND THIRD_PARTY_LIBRARY ${NNDEPLOY_PLUGIN_THIRD_PARTY_LIBRARY})
list(APPEND THIRD_PARTY_LIBRARY ${NNDEPLOY_PLUGIN_LIST})
target_link_libraries(${BINARY} PUBLIC ${THIRD_PARTY_LIBRARY})

# install
#if(SYSTEM.Windows)
#  install(TARGETS ${BINARY} RUNTIME DESTINATION ${NNDEPLOY_INSTALL_BIN_PATH})
#else()
#  install(TARGETS ${BINARY} RUNTIME DESTINATION ${NNDEPLOY_INSTALL_LIB_PATH})
#endif()

# unkown
if("${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" STREQUAL "")
    add_custom_command(TARGET ${BINARY} POST_BUILD 
        COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/nndeploy/_nndeploy_internal${PYTHON_MODULE_PREFIX}${PYTHON_MODULE_EXTENSION} 
        ${PROJECT_SOURCE_DIR}/python/nndeploy/_nndeploy_internal${PYTHON_MODULE_PREFIX}${PYTHON_MODULE_EXTENSION})
        message(STATUS "Copying ${CMAKE_CURRENT_BINARY_DIR}/nndeploy/_nndeploy_internal${PYTHON_MODULE_PREFIX}${PYTHON_MODULE_EXTENSION} to ${PROJECT_SOURCE_DIR}/python/nndeploy/_nndeploy_internal${PYTHON_MODULE_PREFIX}${PYTHON_MODULE_EXTENSION}")
endif("${CMAKE_LIBRARY_OUTPUT_DIRECTORY}" STREQUAL "")

# 生成python pip package安装脚本
configure_file(${ROOT_PATH}/python/setup.py.cfg ${PROJECT_SOURCE_DIR}/python/setup.py)

# unset
unset(SOURCE)
unset(OBJECT)
unset(BINARY)
unset(DIRECTORY)
unset(DEPEND_LIBRARY)
unset(SYSTEM_LIBRARY)
unset(THIRD_PARTY_LIBRARY)